package doc

import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

/**
 * @CreateData:     2019/11/1 9:33
 * @E-Mail:         709674174@qq.com
 * @Author:         lyy
 * @Description:    Kotlin 协程 文档
 */
@Suppress("SpellCheckingInspection")
class GlobalScopeDoc {

    /**
     * 调用runBlocking后，调用线程会阻塞到内部的协程执行完毕
     * @keywords runBlocking 表示一个协程作用域
     * @keywords coroutineScope 表示一个协程作用域
     * @Des runBlocking与coroutineScope主要与别在于后者在等待所有子协程执行完毕时不会阻塞当前线程
     *
     */
    fun startCoroutine() = runBlocking {
        launch {
            delay(100)//延迟
            println("Task from runBlocking")
        }

        //创建一个协程作用域
        coroutineScope {
            launch {
                delay(500)
                println("Task from nested launch")
            }
            delay(100)
            println("Task from coroutine scope")
        }
        println("Coroutine scope is over")
    }

}